package com.cherry.web.controller.system;

import com.cherry.common.config.CherryConfig;
import com.cherry.common.constant.IndexConstants;
import com.cherry.common.core.controller.BaseController;
import com.cherry.common.core.domain.entity.SysUser;
import com.cherry.common.core.text.Convert;
import com.cherry.common.utils.ServletUtils;
import com.cherry.common.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;

/**
 * 首页 控制层
 *
 * @author 孙晨阳
 */
@Controller
public class SysIndexController extends BaseController {

    /**
     * 系统首页
     *
     * @param mmap 跳转至首页时后端传递的信息
     * @return 首页页面
     */
    @GetMapping("index")
    public String index(ModelMap mmap) {
        // 取出当前登录用户的信息
        SysUser user = getSysUser();

        mmap.put("user", user);
        mmap.put("sideTheme", IndexConstants.THEME_DARK);
        mmap.put("skinName", IndexConstants.SKIN_BLUE);
        Boolean footer = Convert.toBool(IndexConstants.FOOTER_TRUE, true);
        Boolean tagsView = Convert.toBool(IndexConstants.TAGS_VIEW_TRUE, true);
        mmap.put("footer", footer);
        mmap.put("tagsView", tagsView);
        mmap.put("mainClass", contentMainClass(footer, tagsView));
        mmap.put("copyrightYear", CherryConfig.getCopyrightYear());
        mmap.put("demoEnabled", CherryConfig.isDemoEnabled());
        mmap.put("isMobile", ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")));

        // 菜单导航显示风格
        String menuStyle = IndexConstants.MENU_STYLE_DEFAULT;
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;

        // 优先Cookie配置的导航菜单风格
        Cookie[] cookies = ServletUtils.getRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (StringUtils.isEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName())) {
                indexStyle = cookie.getValue();
                break;
            }
        }
        return "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";
    }

    /**
     * 切换主题
     *
     * @return
     */
    @GetMapping("/system/switchSkin")
    public String switchSkin() {
        return "skin";
    }

    /**
     * 系统介绍
     *
     * @param mmap
     * @return
     */
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        return "main";
    }

    /**
     * content-main class
     *
     * @param footer   是否开启页脚
     * @param tagsView 是否开启页签
     * @return
     */
    public String contentMainClass(Boolean footer, Boolean tagsView) {
        if (Boolean.TRUE.equals(!footer) && Boolean.TRUE.equals(!tagsView)) {
            return "tagsview-footer-hide";
        } else if (Boolean.FALSE.equals(footer)) {
            return "footer-hide";
        } else if (Boolean.FALSE.equals(tagsView)) {
            return "tagsview-hide";
        }
        return StringUtils.EMPTY;
    }
}
